/**
 * 
 */
package org.gov.vis.data.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.data.controller.AICtlr;
import org.gov.vis.data.controller.AICtlrImpl;
import org.gov.vis.data.dto.AIDto;
import org.gov.vis.data.gui.AIDialog;
import org.gov.vis.gui.util.ErrorsDisplayJPanel;
import org.gov.vis.master.gui.listener.BaseListener;
import org.gov.vis.util.DBConstants;
import org.gov.vis.util.DU;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.Util;

/**
 * @author Vishwanath
 *
 */
public class AIListener implements ActionListener,BaseListener{


	private AIDialog aiDialog;
	private AICtlr aiCtlr = new AICtlrImpl();

	private ErrorsDisplayJPanel errorMsgPanel;
	final private ResourceBundle rb = Util.getResources();
	private AIDto aiDto;

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */

	public AIListener(AIDialog aiDialog){

		this.aiDialog = aiDialog;
	}
	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		try{
			errorMsgPanel = new ErrorsDisplayJPanel(aiDialog.getjPanel1());
			errorMsgPanel.clearMsgPanel();
			
			if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_save"))
					|| actionEvent.getActionCommand().equals(rb.getString("dialog_button_update"))){
				//Method called to either Add or Edit the AI details
				addOrEditAI();
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_view"))){			
				//Method called to view the details of AI Object based on the passed aiId
				viewAI(1);
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_delete"))){
				//Method called to delete the AI Object based on the passed aiId.
				deleteAI(1);
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_reset"))){
				//Method called to clear the screen data
				clear();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}//end actionPerformed

	/**
	 * Method is called to view the AI
	 * @param _aiId
	 */
	private void viewAI(final long _aiId) {
		// TODO Auto-generated method stub
		try{
			final AIDto _aiDto = aiCtlr.getAI(_aiId);
			setDtoDetailsToGui(_aiDto);
			aiDialog.validate();
			aiDialog.repaint();
		}catch(Exception e){
			e.printStackTrace();
		}
	}//end viewAI
	
	/**
	 * Method called to delete the AI Object based on the passed aiId
	 * @param _aiId
	 */
	private void deleteAI(final long  _aiId){
		try{
			aiCtlr.deleteAI(_aiId);
		}catch(Exception e){
			e.getMessage();
		}
	}//end deleteAI
	

	/**
	 * Method called to either Add or Edit the AI details
	 */
	private void addOrEditAI(){
		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);

		//Method to set the Screen's data to  the DTO Object
		setGuiDetailsToDto(aiDialog);
		try{
			//Method called to Add or Edit the AI
			aiCtlr.addOrEditAI(aiDto);
			 /*List<String> successMsgLst = new ArrayList<String>();
			 successMsgLst.add(rb.getString(GuiConstants.DATA_ADDAI_SUCCESS));
			 errorMsgPanel.setSuccessMessages(successMsgLst);
			 */
			 JOptionPane.showMessageDialog(aiDialog, rb.getString(GuiConstants.DATA_ADDAI_SUCCESS));
			// aiDialog.dispose();
				
						//Method called to clear the screen data
			clear();
		}catch(Exception e){
			 List<String> errorMsgLst = new ArrayList<String>();
			if(e instanceof InvalidInputException){
				final HashMap<String,String> _exceptions = AICtlr.errorMsgs;
				if (_exceptions != null && !_exceptions.isEmpty()) {
					String errMsg = null;

					if(_exceptions.containsKey(rb.getString("dialog_label_ai_code").toUpperCase())){
						aiDialog.getLblCode().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_ai_code").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_bullno").toUpperCase())){
						aiDialog.getLblBullNo().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_bullno").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_ai_dtofinsumnaton").toUpperCase())){
						aiDialog.getLblDOI().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_ai_dtofinsumnaton").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					
					if(_exceptions.containsKey(rb.getString("dialog_label_ai_breed").toUpperCase())){
						aiDialog.getLblBreed().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_ai_breed").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_ai_animal_type").toUpperCase())){
						aiDialog.getLblAnimalType().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_ai_animal_type").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_ai_kndofanml").toUpperCase())){
						aiDialog.getLblKindOfAnimal().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_ai_kndofanml").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_ai_brthgvn").toUpperCase())){
						aiDialog.getLblBirthsGiven().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_ai_brthgvn").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_ai_noai").toUpperCase())){
						aiDialog.getLblNameOfAI().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_ai_noai").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_ai_aifee").toUpperCase())){
						aiDialog.getLblAIFee().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_ai_aifee").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					
					errorMsgPanel.setErrorMessages(errorMsgLst);
				} else {
					//log.debug(e.getMessage(), e);
				}	
			}
			else{
				errorMsgLst.add(e.getMessage());
				errorMsgPanel.setErrorMessages(errorMsgLst);

			}
		}		
	}//end addOrEditAI

	/**
	 * Method called to Clear all the form fields of the screen
	 */
	public void clear() {
		// TODO Auto-generated method stub

		//set the Field's data to empty values
		aiDialog.getTxtFldCode().setText("");
		aiDialog.getAnimalType().setSelectedItem(DBConstants.AI_ANIMAL_TYPE_BUFFALOW);
		aiDialog.getTxtFldBrthGvn().setText("");
		aiDialog.getTxtFldBreed().setText("");
		aiDialog.getTxtFldBullNo().setText("");
		aiDialog.getTxtFldCattleOWR().setText("");
		aiDialog.getDtChoDateOfInsemination().setDate(null);
		aiDialog.getTxtFldAIFee().setText("");
		aiDialog.getTxtFldKndOfAnml().setText("");
		aiDialog.getTxtFldMand().setText("");
		aiDialog.getTxtFldNoAI().setText("");
		aiDialog.getTxtFldRecpNo().setText("");
		//aiDialog.getTxtFldTOI().setText("");
		aiDialog.getTxtFldVill().setText("");
		aiDialog.getVisComBoxModel().setSelectedKey(1);
		aiDialog.getVisComBoxModel2().setSelectedKey(0);
		aiDialog.getCombxMeridian().setSelectedIndex(0);
	
		

		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);

		//ResultPanel
		//	aiDialog.getLblResultOrg().setText("");
		//aiDialog.getLblrRsultCode().setText("");

	}//end clear

	/**
	 * Method called to read the data from GUI and set them to DTO Object 
	 * @param _guiObject
	 */
	@Override
	public void setDtoDetailsToGui(Object object) {
		// TODO Auto-generated method stub
		AIDto _aiDto = (AIDto)object;
		aiDialog.getTxtFldCode().setText(_aiDto.getCode());
		aiDialog.getTxtFldAnimalTyp().setText(_aiDto.getAnimalType());
		aiDialog.getTxtFldBrthGvn().setText(_aiDto.getBirthGiven());
		aiDialog.getTxtFldBreed().setText(_aiDto.getBreed());
		aiDialog.getTxtFldBullNo().setText(_aiDto.getBullNo());
		aiDialog.getTxtFldCattleOWR().setText(_aiDto.getCattleOwner());
		aiDialog.getTxtFldDOI().setText(DU.format(_aiDto.getDoi(),DBConstants.DISPLAY_DATE_FORMAT));
		aiDialog.getTxtFldAIFee().setText(_aiDto.getFee());
		aiDialog.getTxtFldKndOfAnml().setText(_aiDto.getKindOfAnimal());
		aiDialog.getTxtFldMand().setText(_aiDto.getMandal());
		aiDialog.getTxtFldNoAI().setText(_aiDto.getNoOfAI());
		aiDialog.getTxtFldRecpNo().setText(_aiDto.getReceiptNo());
		//aiDialog.getTxtFldTOI().setText(_aiDto.getToi());
		aiDialog.getTxtFldVill().setText(_aiDto.getVillage());
		aiDialog.getCombxHours().setSelectedIndex(Integer.parseInt(_aiDto.getToi().getHour()));
		aiDialog.getCombxMinutes().setSelectedIndex(Integer.parseInt(_aiDto.getToi().getMinute()));
		aiDialog.getCombxMeridian().setSelectedItem(_aiDto.getToi().getMeridian());

	}

	/**
	 * Method called to read the data from DTO and set them to GUI.
	 * @param _guiObject
	 */
	@Override
	public void setGuiDetailsToDto(Object object) {
		// TODO Auto-generated method stub
		final AIDialog _aiDialog = (AIDialog)object;
		aiDto = new AIDto();
		aiDto.setCode(_aiDialog.getTxtFldCode().getText());
		aiDto.setAnimalType(_aiDialog.getAnimalType().getSelectedItem().toString());
		aiDto.setBirthGiven(_aiDialog.getTxtFldBrthGvn().getText());
		aiDto.setBreed(_aiDialog.getTxtFldBreed().getText());
		aiDto.setBullNo(_aiDialog.getTxtFldBullNo().getText());
		aiDto.setCattleOwner(_aiDialog.getTxtFldCattleOWR().getText());
		//aiDto.setDoi(DU.parse(_aiDialog.getTxtFldDOI().getText()));
		if(_aiDialog.getDtChoDateOfInsemination().getDate() != null){
			aiDto.setDoi(_aiDialog.getDtChoDateOfInsemination().getDate());
		}
		aiDto.setFee(_aiDialog.getTxtFldAIFee().getText());
		aiDto.setKindOfAnimal(_aiDialog.getTxtFldKndOfAnml().getText());
		aiDto.setMandal(_aiDialog.getTxtFldMand().getText());
		aiDto.setNoOfAI(_aiDialog.getTxtFldNoAI().getText());
		aiDto.setReceiptNo(_aiDialog.getTxtFldRecpNo().getText());
		//aiDto.setToi(_aiDialog.getTxtFldTOI().getText());
		aiDto.setVillage(_aiDialog.getTxtFldVill().getText());
		
		aiDto.getToi().setHour(String.valueOf(_aiDialog.getVisComBoxModel().getSelectedkey()-1));
		aiDto.getToi().setMinute(String.valueOf(_aiDialog.getVisComBoxModel2().getSelectedkey()));
		aiDto.getToi().setMeridian(_aiDialog.getCombxMeridian().getSelectedItem().toString());
		

	}

	public void setAIDialog(AIDialog aiDialog) {
		this.aiDialog = aiDialog;
	}

	public AIDialog getAIDialog() {
		return aiDialog;
	}

	public void setAICtlr(AICtlr aiCtlr) {
		this.aiCtlr = aiCtlr;
	}

	public AICtlr getAICtlr() {
		return aiCtlr;
	}
	@Override
	public void clearGUILabelColors(Color _lblColor) {
		//set the Field's Labels to default Black color
		aiDialog.getLblCode().setForeground(_lblColor);
		aiDialog.getLblAIFee().setForeground(_lblColor);
		aiDialog.getLblAnimalType().setForeground(_lblColor);
		aiDialog.getLblBirthsGiven().setForeground(_lblColor);
		aiDialog.getLblBreed().setForeground(_lblColor);
		aiDialog.getLblMandal().setForeground(_lblColor);
		aiDialog.getLblBullNo().setForeground(_lblColor);
		aiDialog.getLblCattleOwner().setForeground(_lblColor);
		aiDialog.getLblDOI().setForeground(_lblColor);
		aiDialog.getLblKindOfAnimal().setForeground(_lblColor);
		aiDialog.getLblNameOfAI().setForeground(_lblColor);
		aiDialog.getLblReceiptNo().setForeground(_lblColor);
		aiDialog.getLblTOI().setForeground(_lblColor);
		aiDialog.getLblVillage().setForeground(_lblColor);

	}


}//end class
